Skip to content

fix: skip reorder when file has parse errors to prevent data loss#234

Closed
nojaf wants to merge 1 commit into
GDQuest:mainfrom
nojaf:fix-removed-function
Closed

fix: skip reorder when file has parse errors to prevent data loss#234
nojaf wants to merge 1 commit into
GDQuest:mainfrom
nojaf:fix-removed-function

Conversation

@nojaf
Copy link
Copy Markdown

@nojaf nojaf commented May 28, 2026

Please check if the PR fulfills these requirements:

  • The commit message follows our guidelines.
  • For bug fixes and features:
    • You tested the changes.

Related issue (if applicable): #233

What kind of change does this PR introduce?

Bug fix.

Does this PR introduce a breaking change?

No.

New feature or change

What is the current behavior?

When running with --reorder-code on a file that contains invalid GDScript syntax, the formatter silently deletes entire function definitions. For example, a trailing . after a type annotation (var yozora: bool.) causes tree-sitter's error recovery to wrap the surrounding function in an ERROR node. The reorder query uses (_) as a wildcard, which excludes ERROR nodes, so the affected function is never collected and simply disappears from the output.

What is the new behavior?

Before running the reorder step, the formatter checks tree.root_node().has_error(). If parse errors are present, reordering is skipped and the already-formatted code is returned as-is, with a warning printed to stderr. All functions are preserved.

Other information

A regression test has been added in tests/reorder_code/ covering the exact scenario from the issue.
Claude Code was used to create this PR but I did human verify the changes.

  When --reorder-code is used on a file with invalid syntax, tree-sitter
  wraps the affected function in an ERROR node. The reorder query uses
  (_)
  which silently excludes ERROR nodes, causing entire functions to be
  dropped from the output.

  Guard the reorder step with has_error() so files with parse errors are
  left untouched rather than losing declarations silently.
@NathanLovato
Copy link
Copy Markdown
Contributor

Thank you for taking the time to report and try to provide a fix. I don't think this is how this should be addressed, the parse error should not bubble up to the function definition. This is the sort of thing that should be addressed upstream and not hacked around here because this kind of code will hide errors and make us miss opportunities to consolidate the parser for the entire Godot ecosystem.

Also, the parser already have the --safe flag for this sort of case, which will prevent the formatter from running until you've addressed the syntax error. Running the formatter without this option is something I'd mostly recommended to people who want to contribute to this project and help improve the quality of the tree-sitter parser (which in turn improves GDScript support in third party code editors) and of the formatter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants